const Calc = {
    /**
     * 乘法函数，用来得到精确的乘法结果
     * @param arg1
     * @param arg2
     * @param d
     */
    Mul: function (arg1: number, arg2: number, d: number = 2) {
        let r1 = arg1.toString(), r2 = arg2.toString(), m, resultVal;
        m = (r1.split(".")[1] ? r1.split(".")[1].length : 0) + (r2.split(".")[1] ? r2.split(".")[1].length : 0);
        resultVal = Number(r1.replace(".", "")) * Number(r2.replace(".", "")) / Math.pow(10, m);
        return Number(resultVal.toFixed(d));
    },
    /**
     * 除法函数，用来得到精确的除法结果
     * @param arg1
     * @param arg2
     * @param d
     * @constructor
     */
    Div: function (arg1: number, arg2: number, d: number = 2) {
        let r1 = arg1.toString(), r2 = arg2.toString(), m, resultVal;
        m = (r2.split(".")[1] ? r2.split(".")[1].length : 0) - (r1.split(".")[1] ? r1.split(".")[1].length : 0);
        resultVal = Number(r1.replace(".", "")) / Number(r2.replace(".", "")) * Math.pow(10, m);
        return Number(resultVal.toFixed(d));
    },
    /**
     * 加法函数，用来得到精确的加法结果
     * @param arg1
     * @param arg2
     * @param d
     * @constructor
     */
    Add: function (arg1: number, arg2: number, d: number = 2) {
        let r1 = arg1.toString(), r2 = arg2.toString();
        let arg1Arr = r1.split("."), arg2Arr = r2.split("."), d1 = arg1Arr.length == 2 ? arg1Arr[1] : "", d2 = arg2Arr.length == 2 ? arg2Arr[1] : "";
        let maxLen = Math.max(d1.length, d2.length);
        let m = Math.pow(10, maxLen);
        let result = Number(((arg1 * m + arg2 * m) / m).toFixed(maxLen));
        return Number((result).toFixed(d));
    },
    /**
     * 减法函数，用来得到精确的减法结果
     * @param arg1
     * @param arg2
     * @param d
     * @constructor
     */
    Sub: function (arg1: number, arg2: number, d: number = 2) {
        return Calc.Add(arg1, -Number(arg2), d);
    },
}

export default  Calc;
